## This file creates all tables in the main body and supplementary materials.
setwd("/Users/agunderson/Dropbox/Book Project")

#clear working environment
rm(list=ls())

library(lfe)
library(tidyverse)
library(readxl)
library(xtable)
library(stargazer)
library(AER)
library(openintro)

##################################################################
# Table 1 in the paper --------------
##################################################################  
facilities <- read_excel("./Data/facilitydatabase.xlsx") %>%
  mutate(numberprimarycustomers = count.fields(textConnection(primarycustomerall), sep = ","),
         designcapacity = replace(designcapacity,is.na(designcapacity),0),
         stateonly = ifelse(grepl("State",customertype),1,0),
         moreonecustomer = ifelse(grepl(",",primarycustomerall),1,0)) #replace CCA with CoreCivic and GEO with GEO Group

facilities.company <- facilities %>%
  group_by(company) %>%
  dplyr::summarise(`Number of Unique Facilities`=length(unique(facilityname)),
            `Sum Capacity` = sum(designcapacity))%>%
  dplyr::rename(Company=company) 

x <- xtable(facilities.company, caption= "Table 1: Original dataset of private prisons by company, 1986 to 2016. CoreCivic is included from 1986 to 2016, GEO Group is included from 1989 to 2016, Cornell from 1996 to 2009, and CSC from 1997 to 2004.", 
            label="facilitiestable",digits=c(0,0,0,0))
print(x, type="html", file="./Tables/facilitiestable.html",include.rownames=F)

##################################################################
# Table 2 in the paper --------------
##################################################################  
fjcandprivatization <- read_csv("./Data/POPdata.csv") %>%
  mutate(lagsumfacilitiesstateonly = if_else(is.na(lagsumfacilitiesstateonly),0,lagsumfacilitiesstateonly))

facilities.long <- separate_rows(facilities, primarycustomerall,sep=", ") %>%
  mutate(designcapacity2 = designcapacity/numberprimarycustomers)

facilities.state <- facilities.long %>% #first year with private prison in borders
  group_by(state) %>%
  filter(customertype=="State")%>%
  dplyr::summarise(`First Private Prison`=min(year)) %>%
  right_join(.,fjcandprivatization %>% select(jurisdiction) %>% unique(),by=c("state"="jurisdiction"))

facilities.prisoners <- facilities.long %>%  
  group_by(primarycustomerall) %>%
  dplyr::summarise(`First Private Inmates`=min(year))

facilities.table <- left_join(facilities.state,facilities.prisoners,by=c("state"="primarycustomerall")) %>%
  dplyr::rename(State=state) %>%
  arrange(State) 

x2 <- xtable(facilities.table, caption= "Table 2: Original dataset of private prisons by state, 1986 to 2016. First Private Prison 
       refers to the first year the state opened its first private prison inside its borders. First Private Inmates 
       refers to the first year the state housed any inmates in private facilities, inside or outside their borders.", 
             label="facilitiesbystate",digits=c(0,0,0,0))
print(x2, type="html", file="./Tables/facilitiesbystate.html",include.rownames=F)

##################################################################
# Table 3 in the paper --------------
##################################################################  
DCnolawsuits <- felm(lagDC~repcontrol*repgov+budget.gapPC+unionized+incar.rate.y+violcrimerate|
                       jurisdiction+year|
                       0|jurisdiction,data=fjcandprivatization)
summary(DCnolawsuits)

DCmain <- felm(lagDC~sumlawsuitsall+repcontrol*repgov+budget.gapPC+unionized+incar.rate.y+violcrimerate|
                 jurisdiction+year|
                 0|jurisdiction,data=fjcandprivatization)
summary(DCmain)

stargazer(DCnolawsuits,DCmain,no.space = T,covariate.labels = c("Sum Lawsuits","Republican Legislature",
                                                                "Republican Governor",
                                                                "Unified Rep. Gov't",
                                                                "Budget Gap Per Capita",
                                                                "# Unionized Corrections Officers (Thousands)",
                                                                "Incarceration Rate",
                                                                "Violent Crime Rate"),
          title="OLS Model of Level of Prison Privatization",style="apsr",
          notes = "SE's clustered by state.",label = "olsmain",dep.var.labels = "Private Design Capacity",
          out="./Tables/olsmain.html",order=c(1,2,3,8,4,5,6,7))

##################################################################
# Table 4 in the paper --------------
##################################################################  
fjcandprivatization <- fjcandprivatization %>%
  mutate(CIRCUIT = as.factor(CIRCUIT),
         year=as.factor(year)) 

iv_function <- function(dv,independent,instrumental,depvarlabel,indvarlabel,ivlabel,tablelabel,tabletitle){
  x1 <- felm(dv~independent+CIRCUIT+year|0|0|CIRCUIT, data=fjcandprivatization,
             psdef=FALSE)
  
  x2 <- felm(dv~CIRCUIT+year|0|(independent~instrumental)|CIRCUIT, data=fjcandprivatization,
             psdef=FALSE) 
  
  fstat <- ivreg(dv ~ CIRCUIT + year + independent | CIRCUIT + year +
                   instrumental, data=fjcandprivatization)
  print(summary(fstat,vcov = sandwich, diagnostics = TRUE))  #f-stat of 19.323  
  
  rownames(x2$coefficients)[rownames(x2$coefficients)=='`independent(fit)`']<-"independent"
  rownames(x2$beta)[rownames(x2$beta)=='`independent(fit)`']<-"independent"
  
  stargazer(x1,x2$stage1,x2,
            column.labels=c("OLS","First Stage IV","IV"),dep.var.labels = c(depvarlabel,
                                                                            indvarlabel,
                                                                            depvarlabel),
            style="apsr",label=tablelabel,out=paste0("./Tables/",tablelabel,".html"),
            title=tabletitle,
            notes = "SE's clustered by circuit.",
            keep = c("independent","instrumental","Constant"),
            covariate.labels=c(indvarlabel,ivlabel))
}

iv_function(fjcandprivatization$lagDC,fjcandprivatization$sumlawsuitsall,fjcandprivatization$WeightperJudgeServing,
            "Lagged Private DC","Sum Lawsuits","Weight per Judge Serving","hyp2DC",
            "Table 4: The Effect of Terminated Prisoner Lawsuits on Lagged Private Design Capacity")

fstat <- ivreg(lagDC ~ CIRCUIT + year + sumlawsuitsall | CIRCUIT + year +
                 WeightperJudgeServing, data=fjcandprivatization)
summary(fstat,vcov = sandwich, diagnostics = TRUE)  #f-stat of 19.323 

##################################################################
# Supplementary Table 1 --------------
##################################################################  
propDCapp <- felm(lagpropDC~sumlawsuitsall+repcontrol*repgov+budget.gapPC+unionized+incar.rate.y+violcrimerate|
                    jurisdiction+year|
                    0|jurisdiction,data=fjcandprivatization)
summary(propDCapp)

stargazer(propDCapp,no.space = T,covariate.labels = c("Sum Lawsuits",
                                                      "Republican Legislature",
                                                      "Republican Governor",
                                                      "Budget Gap Per Capita",
                                                      "# Unionized Corrections Officers (Thousands)",
                                                      "Incarceration Rate",
                                                      "Violent Crime Rate",
                                                      "Unified Rep. Gov't"),
          title="OLS Model of Proportion of Corrections System that is Private",style="apsr",
          notes = "SE's clustered by state.",label = "propDCapp",dep.var.labels = "Prop. in Private Facilities",
          out="./Tables/propDCapp.html")

##################################################################
# Supplementary Table 2 --------------
##################################################################  
sumfacilitiesapp <- felm(lagsumfacilitiesstateonly~sumlawsuitsall+repcontrol*repgov+budget.gapPC+unionized+incar.rate.y+violcrimerate|
                           jurisdiction+year|
                           0|jurisdiction,data=fjcandprivatization)
summary(sumfacilitiesapp)

stargazer(sumfacilitiesapp,no.space = T,covariate.labels = c("Sum Lawsuits",
                                                             "Republican Legislature",
                                                             "Republican Governor",
                                                             "Budget Gap Per Capita",
                                                             "# Unionized Corrections Officers (Thousands)",
                                                             "Incarceration Rate",
                                                             "Violent Crime Rate",
                                                             "Unified Rep. Gov't"),
          title="OLS Model of Sum State Private Facilities",style="apsr",
          notes = "SE's clustered by state.",label = "sumfacilitiesapp",
          dep.var.labels = "Sum of State Private Facilities",out="./Tables/sumfacilitiesapp.html")

##################################################################
# Supplementary Table 3 --------------
##################################################################  
smalllawsuits <- felm(lagDC~sumlawsuitssmall+repcontrol*repgov+budget.gapPC+unionized+incar.rate.y+violcrimerate|
                        jurisdiction+year|
                        0|jurisdiction,data=fjcandprivatization)
summary(smalllawsuits)

biglawsuits <- felm(lagDC~sumlawsuitsbig+repcontrol*repgov+budget.gapPC+unionized+incar.rate.y+violcrimerate|
                      jurisdiction+year|
                      0|jurisdiction,data=fjcandprivatization)
summary(biglawsuits)

stargazer(smalllawsuits,biglawsuits,no.space = T,covariate.labels = c("Sum Small Lawsuits",
                                                                      "Sum Big Lawsuits",
                                                                      "Republican Legislature",
                                                                      "Republican Governor",
                                                                      "Budget Gap Per Capita",
                                                                      "# Unionized Corrections Officers (Thousands)",
                                                                      "Incarceration Rate",
                                                                      "Violent Crime Rate",
                                                                      "Unified Rep. Gov't"),
          title="OLS Model of Level of Prison Privatization Using Alternative Independent Variables",style="apsr",
          notes = "SE's clustered by state.",label = "altIVsols",
          dep.var.labels = "Private Design Capacity",out="./Tables/altIVsols.html")

##################################################################
# Supplementary Table 4 --------------
##################################################################   
census1995 <- read_csv("./Data/1995data.csv") %>%
  group_by(V2,V10)%>%
  dplyr::summarise(sumtotal=sum(V34)) %>% #rated capacity 
  spread(V2,sumtotal) %>%
  group_by(V10) %>%
  replace(is.na(.), 0) %>%
  mutate(percfederal = `6`/sum(`0`,`2`,`6`,na.rm=T),
         state=abbr2state(V10))

fedandfjc <- left_join(fjcandprivatization,census1995,by=c("jurisdiction"="state")) %>%
  mutate(sumlawsuits.stateonly = (1-percfederal) * sumlawsuitsall)

statelawsuits <- felm(lagDC~sumlawsuits.stateonly+repcontrol*repgov+budget.gapPC+unionized+incar.rate.y+violcrimerate|
                        jurisdiction+year|
                        0|jurisdiction,data=fedandfjc)
summary(statelawsuits)

stargazer(statelawsuits,no.space = T,covariate.labels = c("Sum Lawsuits (State Only)", 
                                                          "Republican Legislature",
                                                          "Republican Governor",
                                                          "Budget Gap Per Capita",
                                                          "# Unionized Corrections Officers (Thousands)",
                                                          "Incarceration Rate",
                                                          "Violent Crime Rate",
                                                          "Unified Rep. Gov't"),
          title="OLS Model of Level of Prison Privatization Using Sum State Lawsuits",style="apsr",
          notes = "SE's clustered by state.",label = "olsstatelawsuitsonly",
          dep.var.labels = "Private Design Capacity",out="./Tables/olsstatelawsuitsonly.html")

##################################################################
# Supplementary Table 5 --------------
##################################################################  
DCapp <- felm(lagDC~sumlawsuitsall+legideology*repgov+budget.gapPC+unionized+incar.rate.y+violcrimerate|
                jurisdiction+year|
                0|jurisdiction,data=fjcandprivatization)
summary(DCapp)

stargazer(DCapp,no.space = T,covariate.labels = c("Sum Lawsuits",
                                                  "Legislative Ideology (Shor and McCarty)",
                                                  "Republican Governor",
                                                  "Budget Gap Per Capita",
                                                  "# Unionized Corrections Officers (Thousands)",
                                                  "Incarceration Rate",
                                                  "Violent Crime Rate",
                                                  "Rep. Gov * Leg. Ideology"),
          title="OLS Model of Level of Prison Privatization Adding Shor and McCarty (2011) Legislative Ideology",
          style="apsr",
          notes = "SE's clustered by state.",label = "appshor",dep.var.labels = "Private Design Capacity",
          out="./Tables/appshor.html")

##################################################################
# Supplementary Table 6 --------------
##################################################################   
stateunionapp <- felm(lagDC~sumlawsuitsall+repcontrol*repgov+budget.gapPC+public.mem+incar.rate.y+violcrimerate|
                        jurisdiction+year|
                        0|jurisdiction,data=fjcandprivatization)
summary(stateunionapp)

stargazer(stateunionapp,no.space = T,covariate.labels = c("Sum Lawsuits",
                                                          "Republican Legislature",
                                                          "Republican Governor",
                                                          "Budget Gap Per Capita",
                                                          "% Public Workforce that are Union Members",
                                                          "Incarceration Rate",
                                                          "Violent Crime Rate",
                                                          "Unified Rep. Gov't"),
          title="OLS Model of Level of Prison Privatization Adding State Public Union Membership",
          style="apsr",out="./Tables/stateunionapp.html",
          notes = "SE's clustered by state.",label = "stateunionapp",dep.var.labels = "Private Design Capacity")

##################################################################
# Supplementary Table 7 --------------
################################################################## 
fjcandprivatizationpost1999 <- fjcandprivatization %>% filter(as.numeric(as.character(year))>1999)

DCnolawsuits <- felm(lagDC~repcontrol*repgov+budget.gapPC+unionized+incar.rate.y+violcrimerate+percovercrowded+
                       deathsincustody+policyeconlib_est|
                       jurisdiction+year|
                       0|jurisdiction,data=fjcandprivatization)
summary(DCnolawsuits)

DCnolawsuits2 <- felm(lagDC~repcontrol*repgov+budget.gapPC+unionized+incar.rate.y+violcrimerate+percovercrowded+
                        deathsincustody+policyeconlib_est+sumcontributions|
                        jurisdiction+year|
                        0|jurisdiction,data=fjcandprivatizationpost1999)
summary(DCnolawsuits2)

DCmain <- felm(lagDC~sumlawsuitsall+repcontrol*repgov+budget.gapPC+unionized+incar.rate.y+violcrimerate+percovercrowded+
                 deathsincustody+policyeconlib_est|
                 jurisdiction+year|
                 0|jurisdiction,data=fjcandprivatization)
summary(DCmain)

DCmain2 <- felm(lagDC~sumlawsuitsall+repcontrol*repgov+budget.gapPC+unionized+incar.rate.y+violcrimerate+percovercrowded+
                  deathsincustody+policyeconlib_est+sumcontributions|
                  jurisdiction+year|
                  0|jurisdiction,data=fjcandprivatizationpost1999)
summary(DCmain2)

stargazer(DCnolawsuits,DCnolawsuits2,DCmain,DCmain2,no.space = T,covariate.labels = c("Sum Lawsuits","Republican Legislature",
                                                                                      "Republican Governor",
                                                                                      "Budget Gap Per Capita",
                                                                                      "# Unionized Corrections Officers (Thousands)",
                                                                                      "Incarceration Rate",
                                                                                      "Violent Crime Rate",
                                                                                      "Percent Overcrowded","Deaths in Custody",
                                                                                      "Economic Policy Liberalism","Sum Contributions",
                                                                                      "Unified Rep. Gov't"),
          title="OLS Model of Level of Prison Privatization, Adding Control Variables",style="apsr",
          notes = "SE's clustered by state.",label = "olsadditionalvars",dep.var.labels = "Private Design Capacity",
          out="./Tables/olsadditionalvars.html")

##################################################################
# Supplementary Table 8 --------------
################################################################## 
fjcandprivatization <- fjcandprivatization %>%
  mutate(prevDC = dplyr::lag(designcapacity2))

DCnolawsuits <- felm(lagDC~repcontrol*repgov+budget.gapPC+unionized+incar.rate.y+violcrimerate+prevDC|
                       jurisdiction+year|
                       0|jurisdiction,data=fjcandprivatization)
summary(DCnolawsuits)

DCmain <- felm(lagDC~sumlawsuitsall+repcontrol*repgov+budget.gapPC+unionized+incar.rate.y+violcrimerate+prevDC|
                 jurisdiction+year|
                 0|jurisdiction,data=fjcandprivatization)
summary(DCmain)

stargazer(DCnolawsuits,DCmain,no.space = T,covariate.labels = c("Sum Lawsuits","Republican Legislature",
                                                                "Republican Governor",
                                                                "Budget Gap Per Capita",
                                                                "# Unionized Corrections Officers (Thousands)",
                                                                "Incarceration Rate",
                                                                "Violent Crime Rate",
                                                                "Design Capacity in $t-1$",
                                                                "Unified Rep. Gov't"),
          title="OLS Model of Level of Prison Privatization, Lagged Dependent Variable Model",style="apsr",
          notes = "SE's clustered by state.",label = "olslaggeddv",dep.var.labels = "Private Design Capacity",
          out="./Tables/olslaggeddv.html")

##################################################################
# Supplementary Table 9 --------------
################################################################## 
iv_function(fjcandprivatization$lagpropDC,fjcandprivatization$sumlawsuitsall,fjcandprivatization$WeightperJudgeServing,
            "Lagged Prop. in Private","Sum Lawsuits","Weight per Judge Serving","hyp2propDC",
            "The Effect of Terminated Prisoner Lawsuits on Lagged Proportion of Inmates in Private Facilities")

##################################################################
# Supplementary Table 10 --------------
################################################################## 
iv_function(fjcandprivatization$lagsumfacilitiesstateonly,fjcandprivatization$sumlawsuitsall,fjcandprivatization$WeightperJudgeServing,
            "Lagged Sum State Facilities","Sum Lawsuits","Weight per Judge Serving","hyp2sumfacilities",
            "The Effect of Terminated Prisoner Lawsuits on Lagged Sum State Private Facilities")

##################################################################
# Supplementary Table 11 --------------
################################################################## 
iv_function(fjcandprivatization$lagsumfacilities,fjcandprivatization$sumlawsuitsall,fjcandprivatization$WeightperJudgeServing,
            "Lagged Sum Facilities","Sum Lawsuits","Weight per Judge Serving","hyp2sumALLfacilities",
            "The Effect of Terminated Prisoner Lawsuits on Lagged Sum Private Facilities (All Included)")

##################################################################
# Supplementary Table 12 --------------
################################################################## 
iv_function(fjcandprivatization$lagDC,fjcandprivatization$sumlawsuitssmall,fjcandprivatization$WeightperJudgeServing,
            "Lagged Private DC","Sum Lawsuits","Weight per Judge Serving","hyp2DCsmall",
            "The Effect of Terminated Prisoner Lawsuits (Small) on Lagged Private Design Capacity")

##################################################################
# Supplementary Table 13 --------------
################################################################## 
iv_function(fjcandprivatization$lagDC,fjcandprivatization$sumlawsuitsbig,fjcandprivatization$WeightperJudgeServing,
            "Lagged Private DC","Sum Lawsuits","Weight per Judge Serving","hyp2DCbig",
            "The Effect of Terminated Prisoner Lawsuits (Big) on Lagged Private Design Capacity")

##################################################################
# Supplementary Table 14 --------------
################################################################## 
iv_function(fedandfjc$lagDC,fedandfjc$sumlawsuits.stateonly,fedandfjc$WeightperJudgeServing,
            "Lagged Private DC","Sum Lawsuits (State Only)","Weight per Judge Serving","hyp2DCstateonlylawsuits",
            "The Effect of Terminated Prisoner Lawsuits (State Only) on Lagged Private Design Capacity")

##################################################################
# Supplementary Table 15 --------------
################################################################## 
iv_function(fedandfjc$lagDC,fedandfjc$sumlawsuitsall,fedandfjc$WeightperActiveJudgeServing,
            "Lagged Private DC","Sum Lawsuits","Weight per Authorized Judge Serving","hyp2DCauthorized",
            "The Effect of Terminated Prisoner Lawsuits on Lagged Private Design Capacity, Alternative Instrumental Variable")

##################################################################
# Supplementary Table 16 --------------
################################################################## 
x1 <- felm(lagDC~sumlawsuitsall+total.pop+CIRCUIT+year|0|0|CIRCUIT, data=fjcandprivatization,
           psdef=FALSE)
summary(x1)

x2 <- felm(lagDC~total.pop+CIRCUIT+year|0|(sumlawsuitsall~WeightperJudgeServing)|CIRCUIT, data=fjcandprivatization,
           psdef=FALSE)
summary(x2)  

rownames(x2$coefficients)[rownames(x2$coefficients)=='`sumlawsuitsall(fit)`']<-'sumlawsuitsall'
rownames(x2$beta)[rownames(x2$beta)=='`sumlawsuitsall(fit)`']<-'sumlawsuitsall'

stargazer(x1,x2$stage1,x2,title="The Effect of Terminated Prisoner Lawsuits on Lagged Private Design Capacity, Adding Population",
          column.labels=c("OLS","First Stage IV","IV"),dep.var.labels = c("Lagged Private DC",
                                                                          "Sum Lawsuits",
                                                                          "Lagged Private DC"),
          keep = c("sumlawsuitsall","total.pop","WeightperJudgeServing","Constant"),
          covariate.labels=c("Sum Lawsuits","Population","Weight per Judge Serving"),
          style="apsr",label="hyp2DCpop",out="./Tables/hyp2DCpop.html") 

##################################################################
# Supplementary Table 17 --------------
################################################################## 
fjcandprivatization <- fjcandprivatization %>%
  mutate(logsumlawsuits = log(sumlawsuitsall))

iv_function(fjcandprivatization$lagDC,fjcandprivatization$logsumlawsuits,fjcandprivatization$WeightperJudgeServing,
            "Lagged Private DC","Logged Sum Lawsuits","Weight per Judge Serving","hyp2DClogsum",
            "The Effect of Logged Terminated Prisoner Lawsuits on Lagged Private Design Capacity")

##################################################################
# Supplementary Table 18 --------------
################################################################## 
iv_function(fjcandprivatization$lagDCweighted,fjcandprivatization$sumlawsuitsall,fjcandprivatization$WeightperJudgeServing,
            "Lagged Private DC (Weighted)","Logged Sum Lawsuits","Weight per Judge Serving","hyp2DCweighted",
            "The Effect of Terminated Prisoner Lawsuits on Lagged Private Design Capacity (Weighted)")

